14. Exercise: Add DataBinding to the Adapter
L7 24 Using DataBinding In A RecyclerView SC
Now it’s your turn to complete this exercise yourself!
In this exercise you'll take the Adapter you finished in the last exercise and update it to use Data Binding. This is a refactor, and it won't change the behavior of your list.
In
list_item_sleep_night.xml, wrap theConstraintLayoutin alayouttag.You can use the intention menu via
Alt+Enterto "Convert to data binding layout".
Add a data tag and a variable called
sleep, then rebuild the app.<data> <variable name=”sleep” type="com.example.android.trackmysleepquality.database.SleepNight" /> </data>
In SleepNightAdapter.ViewHolder
In the
companion object, ReplaceLayoutInflaterwithListItemSleepNightBinding:class ViewHolder private constructor(val binding: ListItemSleepNightBinding)In the
from()function, useListItemSleepNightBinding.inflateto create a binding object.fun from(parent: ViewGroup): ViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = ListItemSleepNightBinding.inflate(layoutInflater, parent, false) return ViewHolder(binding) }Refactor and rename the ViewHolder class’s constructor parameter to take a
ListItemSleepNightBindingparameter calledbinding.root.Remember to pass the parameter to the parent constructor:
class ViewHolder private constructor(val binding: ListItemSleepNightBinding): RecyclerView.ViewHolder(binding.root) { }Note Your app won't compile after this step. We'll fix that next.
In
ViewHolder, replacefindViewByIdcalls with references to binding object fields, then inline them:
val sleepLength: TextView = binding.sleepLength
val quality: TextView = binding.qualityString
val qualityImage: ImageView = binding.qualityImage
Once your code is compiling you can inline the new definitions. Do this by right clicking on each field, then selecting Refactor > Inline. Select Inline all references and remove the property to tell Android studio to remove the property.
Inline is a very common refactor. It's worth taking a moment to learn the keyboard shortcut. You can find the keyboard shortcut in the context menu next to Inline.
Your refactored ViewHolder should now look like this:
class ViewHolder private constructor(val binding: ListItemSleepNightBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(item: SleepNight) {
val res = itemView.context.resources
binding.sleepLength.text = convertDurationToFormatted(item.startTimeMilli, item.endTimeMilli, res)
binding.qualityString.text = convertNumericQualityToString(item.sleepQuality, res)
binding.qualityImage.setImageResource(when (item.sleepQuality) {
0 -> R.drawable.ic_sleep_0
1 -> R.drawable.ic_sleep_1
2 -> R.drawable.ic_sleep_2
3 -> R.drawable.ic_sleep_3
4 -> R.drawable.ic_sleep_4
5 -> R.drawable.ic_sleep_5
else -> R.drawable.ic_sleep_active
})
}
companion object {
fun from(parent: ViewGroup): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ListItemSleepNightBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
}
}
- Build and run the app, and verify the refactor has not changed anything visible to the user.
If you want to start at this step, you can download this exercise from: Step.08-Exercise-Add-DataBinding-to-Adapter.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here: Step.08-Solution-Add-DataBinding-to-Adapter, or using this git diff.
Task Description:
Complete these steps to add DataBinding and use it in the ViewHolder.
Task Feedback:
Congrats! You've implemented data binding in your RecyclerView Adapter!